/**
 *  Module
 *
 * Description
 */
angular.
module('sfhtHaitaoCmsApp')
    //编辑模块数据
    .controller('ModalDataCtrl', function($scope, $injector, $modal, $modalInstance, $filter, item, nations, Uploader) {
        var commonBizService = $injector.get("CommonBiz");

        function convertDateToString(dateValue) {
            return $filter('date')(dateValue, 'yyyy-MM-dd HH:mm');
        }
        $scope.items = item.properties;
        $scope.nations = nations;
        _.each($scope.items, function(value, index) {
            if (value.field.fieldType == "Date") {
                $scope.items[index].propertyValue = convertDateToString(value.propertyValue);
            }
            if(value.propertyName == "itemId" && value.propertyValue == 0){
                value.propertyValue = "";
            }
        });

        /**
         * 上传图片
         * @param  {object} 对象
         * @return {array}
         */
        $scope.changeFile = function(slide) {
            Uploader.uploadFile($scope, slide, "image");
        };

        $scope.parseFieldName = function(field) {
            var fields = JSON.parse(field);
            return fields.fieldDescription;
        };

        function checkProperties(items){
            var item = null;
            var temp = null;
            for(var i = 0, len = items.length; i < len; i++){
                item = items[i];
                if(item.propertyName == "link"){
                    if(!window.checkData.checkLink(item.propertyValue)){
                       temp = item.field.fieldDescription + "非法！！";
                       break;
                    }
                }else if(item.propertyName == "h5link"){
                    if(!window.checkData.checkH5Link(item.propertyValue)){
                       temp = item.field.fieldDescription + "非法！！";
                       break;
                    }
                }
            }
            return temp;
        }


        //保存操作
        $scope.save = function() {
            //校验item，link和h5link
            var errorInfo = checkProperties(item.properties);
            if(errorInfo != null){
                alert(errorInfo);
                return false;
            }
            commonBizService.updateSingleCmsData(item)
                .then(function(data) {
                    alert("编辑成功！！");
                    $modalInstance.close(data);
                }, function(data) {
                    alert("编辑失败！！" + data);
                    console.log("error:" + data);
                })
        };

        $scope.cancel = function() {
            // window.location.reload(true);
            $modalInstance.dismiss('cancel');
        };

        $scope.isProduct = function(items) {
            var item = _.findWhere(items, {
                'propertyName': 'itemId'
            });
            if (item) {
                return true;
            }
        };

        //搜索操作
        $scope.searchItems = function(itemId, $event) {
            $event.preventDefault();
            commonBizService.getItems({
                    searchKeywords: "item:" + itemId
                })
                .then(function(data) {
                    _.each($scope.items, function(item) {
                        if (item.propertyName != "tag") {
                            var origin = item.propertyName.indexOf('_') == 0 ? item.propertyName.slice(1, item.propertyName.length) : item.propertyName
                            item.propertyValue = data.value[0][item.propertyName] || data.value[0][origin]
                        }
                    });
                    console.log(data);
                }, function(data) {
                    console.log(data);
                })
        };
        //日历
        $scope.initTime = function() {
            $('.datetimepicker').datetimepicker({
                format: 'YYYY-MM-DD HH:mm'
            })
        };

        $scope.dateChange = function(item, index) {
            item.propertyValue = $('.datetimepicker' + index).val()
        }
    })
